{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div id="tip_modal">
    <form class="layui-form p-4">
        <h3 class="pb-3">功能菜单/节点</h3>
        <table class="layui-table layui-table-form">
            <tr>
                <td class="layui-td-gray">盘点仓库<font>*</font>
                <td>
                    {$detail.warehouseName}
                </td>
                <td class="layui-td-gray">出库时间<font>*</font>
                </td>
                <td>
                    {$detail.inventoryTime}
                </td>
            </tr>



            <tr>
                <td>盘点人</td>
                <td>
                    <span>{$detail.username|default=''}</span>
                </td>
                <td class="layui-td-gray">状态<font>*</font>
                </td>
                <td>
                    <select name="status" disabled>
                        {foreach [['id'=>1,'name'=>'草稿'],['id'=>2,'name'=>'已确认']] as $st}
                        <option value="{$st.id}" {if isset($detail.status)&&$detail.status==$st.id } selected {/if}>{$st.name}</option>
                        {/foreach}
                    </select>

                </td>

            </tr>
            <tr>

            </tr>
            <tr>
                <td colspan="6"><h3>明细</h3></td>
            </tr>
            <tr>
                <td colspan="6">
                    {if isset($detail.status)&&$detail.status==1}
                    <a href="javascript:"  class="layui-btn layui-btn-primary layui-btn-sm" id="importMx">导入</a>
                    <a href="javascript:" onclick="exportMx()" class="layui-btn layui-btn-primary layui-btn-sm">导入模板</a>
                    <a href="javascript:"  onclick="exportMx()" class="layui-btn layui-btn-primary layui-btn-sm">导出</a>
                    {/if}
                </td>
            </tr>
            <table class="layui-table tableMany" id="inventory_mx_table">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>标记</th>
                    <th>物料编码</th>
                    <th>物料名称</th>
                    <th>规格型号</th>
                    <th>单位</th>
                    <th>仓库</th>
                    <th>系统数量</th>
                    <th>盘点数量</th>
<!--                    <th>操作</th>-->
                </tr>
                </thead>
                {if isset($mxList)}
                <tbody id="mxTbdy">
                {volist name='mxList' id='ml' key='k'}
                <tr>
                    <td><span class="xuhao">{$k}</span>
                        <input type="hidden" name="mxid[]" value="{$ml.id}">
                        <input type="hidden" name="material_id[]" value="{$ml.material_dossier_id}">
                        <input type="hidden" name="incom_receipt_detail_id[]" value="{$ml.incom_receipt_detail_id}">
                    </td>

                    <td>{$ml.dKey}</td>
                    <td>{$ml.bar_code}</td>
                    <td>{$ml.mdName}</td>
                    <td>{$ml.specs}</td>
                    <td>
                        {$ml.unit}
                    </td>
                    <td>
                        {$ml.warehouseName}
                    </td>
                    <td>{$ml.backNum}</td>
                    <td>{$ml.num}</td>
<!--                    <td>-->
<!--                        {if !isset($detail.status)||$detail.status==1}-->
<!--                        <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"-->
<!--                           onclick="delMx('{$ml.id}',this)">删除</a>-->
<!--                        {/if}-->
<!--                    </td>-->
                </tr>
                {/volist}
                </tbody>
                {else}
                <tbody id="mxTbdy">
                </tbody>
                {/if}

            </table>
        </table>
        <div class="py-3">
            <input type="hidden" name="id" value="{$id}">
<!--            {if !isset($detail.status)||$detail.status==1}-->
<!--            <button class="layui-btn layui-btn-normal" type="button" id="sub" lay-submit="" lay-filter="webform1">保存 </button>-->
<!--            {/if}-->
            {if isset($detail.status)&&$detail.status==1}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSure()">确认</button>
            {/if}

        </div>
    </form>
</div>

{/block}
<!-- /主体 -->
{block name="script"}
<script src="{__STATIC__}/xlsx.full.min.js"></script>
<script>

    const moduleInit = ['tool'];

    let isSub = 2;
    var houseId="{$detail.warehouse_id|default='0'}";

    function gouguInit() {
        var form = layui.form, tool = layui.tool,upload=layui.upload;
        //监听提交
        form.on('submit(webform1)', function (data) {
            let callback = function (e) {
                isSub = 2;
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            if (isSub == 2) {
                isSub = 1
                tool.post("{:url('add')}", data.field, callback);
            }
            return false;
        });

        // layui.use(['form'], function () {
        //     var form = layui.form;
        //     form.render();
        // });
        form.on('select(materRial)', function (data) {
            var selectedTitle = '';
            $('#materRial option').each(function () {
                if ($(this).val() == data.value) {
                    selectedTitle = $(this).attr('title');
                }
            });
            console.log('ttt', selectedTitle)
            $('#spces').text(selectedTitle)
        }); // select(lay-filter属性的值)
        //动态获取联动
        form.on('select(warehouse_id)', function (data) {
            var mxIndex = 1;
            var sid= $(data.elem).attr('id')
            var wid=data.value;
            houseId=wid;
            $('#'+sid+' option').each(function () {
                if ($(this).val() == data.value) {
                    mxIndex = $(this).attr('title');
                }
            });
            $("#locator_"+mxIndex+"").empty();
            var optionStr='';
            if (wid!=''){
                $.ajax({
                    url:'/home/api/getWarHoseDetail',
                    type:'post',
                    dataType:'json',
                    data:{id:wid},
                    success:function (res) {
                        if (res.code==0){
                            let data=res.data;
                            for(var i=0;i<data.length;i++){
                                optionStr+=" <option value='"+data[i].id+"'>"+data[i].name+"</option>";
                            }
                        }else {
                            optionStr+=" <option value=''>暂无数据</option>";
                        }
                        $("#locator_"+mxIndex+"").append(optionStr);
                        form.render();
                    }

                })
            }else {
                optionStr+=" <option value=''>请选择仓库</option>";
                $("#locator_"+mxIndex+"").append(optionStr);
                form.render();
            }


        }); // select(lay-filter属性的值)
        //弹窗
        var treeTable = layui.table, form = layui.form;
        var tableIns = treeTable.render({
            elem: '#mxTable'
            , url: "{:url('addMx')}?warehouse_id="+houseId
            , method: 'post'
            , toolbar: '#toolbarDemo',
            defaultToolbar: []
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left'},
                {fixed: 'left', field: 'id', title: 'ID', unresize: true, hide: true},
                {field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'specs', title: '规格型号'}
                , {field: 'mdName', title: '名称'}
                , {field: 'bar_code', title: '编码'}
                , {field: 'unit', title: '单位'}
                , {field: 'warehouseName', title: '仓库'}
                , {field: 'haveNum', title: '系统数量'}
            ]]
            , page: true
        });
        //监听搜索提交
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        treeTable.on('toolbar(mxTable)', function (obj) {
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch (obj.event) {
                case 'add':
                    break;
                case 'subt':
                    var data = checkStatus.data;
                    var ids = []
                    if (data.length) {
                        data.forEach(function (value, index) {
                            ids.push(value)
                        })
                        saveAddMx(ids)
                        layer.close(layTip)

                    } else {
                        layer.close(layTip)
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            }
            ;
        })
        laydate.render({
            elem: '#inventoryTime'
            , type: 'date',
        });

        upload.render({
            elem: '#importMx'
            ,url: "{:url('importMx')}?id={$id}&wid="+houseId
            ,accept: 'file'
            ,exts: 'xlsx|xls'
            ,done: function(res){
                layer.msg(res.msg)
                setTimeout(function () {
                    parent.parent.location.reload();
                },2000)
            }
        });
    }
</script>
<script>
    function exportMx() {
        var filenameA='盘点明细';
        var type='xlsx'
        var dl='';
        var fn;
        var elt = document.getElementById('inventory_mx_table');
        var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
        return dl ?
            XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }):
            XLSX.writeFile(wb, fn || (filenameA+'.' + (type || 'xlsx')));
    }
    let is_sure=2;
    function onSure() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sure==1){
            return;
        }
        is_sure=1;
        $.ajax({
            url:"{:url('onSure')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sure=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sure=2
            }
        })
    }
</script>
{/block}
<!-- /脚本 -->